package com.prueba.client.configuracion;



import com.prueba.client.configuracion.TablaStakeholders;
import com.prueba.client.servicios.StakeHolderManager;
import com.prueba.compartido.Stakeholder;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;


public class TablaStakeholders extends Canvas{
	static public TablaStakeholders getInstance(){
		if (singleton==null){
			singleton=new TablaStakeholders();
		}
		return singleton;
	}
	static private TablaStakeholders singleton=null;

	private ListGrid stakeholders;
	
	public void actualizarDatos(){
		this.stakeholders.fetchData();
	}
	
	private TablaStakeholders(){
		super();

        
		//this.setCanDragReposition(false);
		
		//Grilla de stakeholders:
		stakeholders=StakeholdersListGrid.getInstance();
		stakeholders.setAutoFetchData(true);
		
		//boton agregar stakeholders:
		IButton addStakeholder = new IButton("Agregar stakeholder");
		addStakeholder.setAutoFit(true); 
		addStakeholder.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {  
                NewStakeholderWindow.getInstance().show();
            } });
        addStakeholder.setLayoutAlign(Alignment.RIGHT); 
        
        
        //boton eliminar stakeholders:
		IButton deleteStakeholder = new IButton("Eliminar stakeholder");
		deleteStakeholder.setAutoFit(true); 
		deleteStakeholder.addClickHandler(new ClickHandler() {  
            public void onClick(ClickEvent event) {
            	//Selecciono el stakholder implicado:
            	Long id = Long.parseLong(StakeholdersListGrid.getInstance().getSelectedRecord().getAttribute("id"));
            	//Lo borro globalmente:
            	StakeHolderManager.deleteStakeholder(id,StakeholdersListGrid.getInstance().getSelectedRecord());
            
            	//TODO Eliminar Stakeholder
            	/*
            	 */
            	
            } });
		deleteStakeholder.setLayoutAlign(Alignment.RIGHT); 
        
        //agregando componentes al canvas:
        VLayout layout=new VLayout();
        	layout.addMember(stakeholders);
    			//stakeholders.setWidth("100%");   
    			//stakeholders.setHeight("100%");  
        	HLayout hlayout=new HLayout();
        		hlayout.addMember(deleteStakeholder);
        		hlayout.addMember(addStakeholder);
        	layout.addMember(hlayout); 
        this.addChild(layout);
        this.setWidth100();  
        this.setHeight100();          
		this.setPadding(5);
	}
	
	
	static class StakeholdersListGrid extends ListGrid{
		static StakeholdersListGrid getInstance(){
			if (singleton==null){
				singleton=new StakeholdersListGrid();
			}
			return singleton;
		}
		static private StakeholdersListGrid singleton=null;
		
		private StakeholdersListGrid(){
		     
		     //Campos de la grilla
	    
			this.setDataSource(com.prueba.client.datos.StakeholderData.getInstance());
			
		
			//this.setAutoFetchData(true);  
			//this.setFields(new ListGridField[]{Fields.stakeholderNombre,Fields.stakeholderPrioridad,Fields.stakeholderCantidad});   

			 //Parametros de la grilla     
		     this.setCanEdit(true);              
		     this.setCanDragResize(true);
		     this.setWidth(300);  
		     this.setHeight(200); 
		     //this.setOverflow(Overflow.AUTO);
		}

	}	

	static class NewStakeholderWindow extends Window{
		static NewStakeholderWindow getInstance(){
			if (singleton==null){
				singleton=new NewStakeholderWindow();
			}
			return singleton;
		}
		static private NewStakeholderWindow singleton=null;
		static private DynamicForm form=null;
		private NewStakeholderWindow(){
	        //parametros de la ventana:
			this.setAutoSize(true);  
	        this.setTitle("Agregar nuevo stakeholder");  
	        //this.setWidth(width);  
	        //this.setHeight(height);  
	        this.setLeft(400);
	        this.setTop(400);
	        this.setCanDragReposition(true);  
	        //this.setCanDragResize(true);  
	        
	        //elementos del formulario:
	        form = new DynamicForm(); 
	        form.setDataSource(com.prueba.client.datos.StakeholderData.getInstance());
	        /*form.setFields(new FormItem[] { 
	            Forms.stakeholderNombre,Forms.stakeholderPrioridad,Forms.stakeholderCantidad
	        });*/
	        
	        //boton de agregar:
	        IButton ok = new IButton("OK");
            ok.setAutoFit(true); 
            ok.addClickHandler(new ClickHandler() {  
                public void onClick(ClickEvent event) {  
                 	
                    	//Creamos un stakeholder con los datos del form y lo guardamos:
                    	Stakeholder nuevoS = new Stakeholder(form.getValueAsString("stakeholder"),form.getValueAsString("cantidad"),form.getValueAsString("prioridad"));
                    	
                    	StakeHolderManager.saveStakeholder(nuevoS);
                    
                    
                    	NewStakeholderWindow.getInstance().hide();
                    	
                    
                	/*try{
                    	String name=(String)(form.getField("stakeholder").getValue());
                    	if(name==null || name.equals(""))
                    		throw new RuntimeException("nombre del stakeholder no definido");
                    	double priority=Double.valueOf((String)(form.getField("prioridad").getValue()));
                    	if(priority<0.0 || priority>1.0)
                    		throw new RuntimeException("La prioridad debe estar definida entre 0 y 1");
                    	int count=Integer.valueOf((String)(form.getField("cantidad").getValue()));
                    	if(count<=0)
                    		throw new RuntimeException("La cantidad de stakeholders debe ser mayor que 0");
                    	StakeholdersListGrid.getInstance().addData(form.getValuesAsRecord());//NO FUNCA
                    	NewStakeholderWindow.getInstance().hide();
                    }catch( RuntimeException e){
                    	SC.warn(e.getMessage());
                    }*/
                    
                } });
            ok.setLayoutAlign(Alignment.RIGHT); 
            
            //agregando componentes a la ventana:
            VLayout layout=new VLayout();
            layout.addMember(form); 
            layout.addMember(ok);
            this.addItem(layout);
            
		}
	}
}
